﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing.Imaging;
using System.Linq.Expressions;

namespace IML.MVC.Models
{
    public class CaptchaResult : ActionResult
    {
        public string _captchaText { get; set; }
        private Captcha _c { get; set; }

        public CaptchaResult(string captchaText)
        {
            _setup(captchaText, 200, 50);
        }

        public CaptchaResult(string captchaText, int width, int height)
        {
            _setup(captchaText, width, height);
        }

        private void _setup(string captchaText, int width, int height)
        {
            _captchaText = captchaText;
            _c = new Captcha();
            _c.Text = _captchaText;
            _c.Width = width;
            _c.Height = height;
            _c.FamilyName = "Century Schoobook";
        }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpContextBase cb = context.HttpContext;
            cb.Response.Clear();
            cb.Response.ContentType = "image/jpeg";
            _c.Image.Save(cb.Response.OutputStream, ImageFormat.Jpeg);
            _c.Dispose();
        }
    }
}